package club.terroir.client.producteur;

import java.util.Comparator;
import java.util.List;

import club.terroir.client.ControlerClubAdmin;
import club.terroir.client.Style;

import com.google.gwt.cell.client.ButtonCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.view.client.ListDataProvider;

public class ContactsListGUI extends VerticalPanel{


	 // Create a data provider.
    ListDataProvider<Contact> dataProvider = new ListDataProvider<Contact>();

	
  static public ContactsListGUI instance = new ContactsListGUI();

  private ContactsListGUI() {
		super();
		Label labelTitre = new Label("Liste des clients");
		labelTitre.setStyleName(Style.Titre);
		CellTable<Contact> table = initTable();
		SimplePager pager = new SimplePager();
	 // Set the cellList as the display.
		pager.setDisplay(table);
		this.add(labelTitre);
		this.add(table);
		this.add(pager);
	}
  


private   CellTable<Contact>  initTable() {

    // Create a CellTable.
    CellTable<Contact> table = new CellTable<Contact>();

    // Create name column.
    TextColumn<Contact> nameColumn = new TextColumn<Contact>() {
      @Override
      public String getValue(Contact contact) {
        return contact.name;
      }
    };
    // Make the name column sortable.
    nameColumn.setSortable(true);
    
    // Create name column.
    TextColumn<Contact> firstnameColumn = new TextColumn<Contact>() {
      @Override
      public String getValue(Contact contact) {
        return contact.firstName;
      }
    };
    // Make the name column sortable.
    firstnameColumn.setSortable(true);
 
    
    // Create name column.
    TextColumn<Contact> emailColumn = new TextColumn<Contact>() {
      @Override
      public String getValue(Contact contact) {
        return contact.email;
      }
    };
    emailColumn.setSortable(true);

   

    // Create address column.
    TextColumn<Contact> addressColumn = new TextColumn<Contact>() {
      @Override
      public String getValue(Contact contact) {
        return contact.address1;
      }
    };
    addressColumn.setSortable(true);
 
    FieldUpdater<Contact, String> fieldUpdaterButton =  new FieldUpdater<Contact, String>() {
	      @Override
	      public void update(int index, Contact contact, String value) {
	     	  ControlerClubAdmin.instance.displayAchatsClient(contact);
	      }
	    };
	 Column<Contact, String> buttonColumn = new Column<Contact, String>(new ButtonCell()) {
        @Override
        public  String getValue(Contact object) {
          return " ";
        }
      };
      buttonColumn.setFieldUpdater(fieldUpdaterButton);
     
    
    
    
    // Add the columns.
      table.addColumn(nameColumn, "Nom");
      table.addColumn(firstnameColumn, "Prenom");
      table.addColumn(addressColumn, "Adresse");
      table.addColumn(emailColumn, "E-Mail");
      table.addColumn(buttonColumn, "Détail");

   
    // Connect the table to the data provider.
    dataProvider.addDataDisplay(table);

    this.initListDataProvider();
  
    List<Contact> list = dataProvider.getList();
    // Add a ColumnSortEvent.ListHandler to connect sorting to the  java.util.List.
    ListHandler<Contact> columnSortHandlerByFirstName = new ListHandler<Contact>(list);
    columnSortHandlerByFirstName.setComparator(firstnameColumn,
        new Comparator<Contact>() {
          public int compare(Contact o1, Contact o2) {
            if (o1 == o2) {
              return 0;
            }

            // Compare the name columns.
            if (o1 != null) {
              return (o2 != null) ? o1.firstName.compareTo(o2.firstName) : 1;
            }
            return -1;
          }
        });
    
    
    table.addColumnSortHandler(columnSortHandlerByFirstName);
   
    
    
    /////
    ListHandler<Contact> columnSortHandlerByEmail = new ListHandler<Contact>(list);
    columnSortHandlerByEmail.setComparator(emailColumn,
        new Comparator<Contact>() {
          public int compare(Contact o1, Contact o2) {
            if (o1 == o2) {
              return 0;
            }

            // Compare the name columns.
            if (o1 != null) {
              return (o2 != null) ? o1.email.compareTo(o2.email) : 1;
            }
            return -1;
          }
        });
    
    
    table.addColumnSortHandler(columnSortHandlerByEmail);

    // Add a ColumnSortEvent.ListHandler to connect sorting to the  java.util.List.
    ListHandler<Contact> columnSortHandlerByName = new ListHandler<Contact>(list);
    columnSortHandlerByName.setComparator(nameColumn,
        new Comparator<Contact>() {
          public int compare(Contact o1, Contact o2) {
            if (o1 == o2) {
              return 0;
            }

            // Compare the name columns.
            if (o1 != null) {
              return (o2 != null) ? o1.name.compareTo(o2.name) : 1;
            }
            return -1;
          }
        });
    
    
    table.addColumnSortHandler(columnSortHandlerByName);

    // We know that the data is sorted alphabetically by default.
    table.getColumnSortList().push(nameColumn);

    ListHandler<Contact> columnSortHandlerByAddress = new ListHandler<Contact>(list);
    columnSortHandlerByAddress.setComparator(addressColumn,
            new Comparator<Contact>() {
              public int compare(Contact o1, Contact o2) {
                if (o1 == o2) {
                  return 0;
                }

                // Compare the name columns.
                if (o1 != null) {
                  return (o2 != null) ? o1.address1.compareTo(o2.address1) : 1;
                }
                return -1;
              }
            });
        
        
    table.addColumnSortHandler(columnSortHandlerByAddress);
    
    return table;
  }

	public void initListDataProvider() {
		  // Add the data to the data provider, which automatically pushes it to the
	    // widget.
	    List<Contact> list = dataProvider.getList();
	    list.clear();
	    for (Contact contact : ContactFactory.instance.listContacts) {
	      list.add(contact);
	    }
	}



	public void addContactToList(Contact c) {
		this.dataProvider.getList().add(c);
	}


}
	

	

